package util;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ListUtil {
    public static int[] parseIntArray(String data) {
        String[] list = data.substring(1, data.length()-1).split(",");
        if(list.length == 1 && list[0].equals("")) return new int[0];
        int[] result = new int[list.length];
        for(int i=0; i<list.length; ++i) {
            if(list[i].trim().length() <= 0) break;
            result[i] = Integer.parseInt(list[i].trim());
        }
        return result;
    }
    public static double[] parseDoubleArray(String data) {
        String[] list = data.substring(1, data.length()-1).split(",");
        if(list.length == 1 && list[0].equals("")) return new double[0];
        double[] result = new double[list.length];
        for (int i = 0; i < list.length; i++) {
            if(list[i].trim().length() <= 0) break;
            result[i] = Double.parseDouble(list[i].trim());
        }
        return result;
    }
    public static char[] parseCharArray(String data) {
        String[] list = data.substring(1, data.length()-1).split(",");
        char[] result = new char[list.length];
        for(int i=0; i<result.length; ++i) {
            if (list[i].trim().length() <= 0) break;
            result[i] = list[i].trim().charAt(1);
        }
        return result;
    }
    public static List<Integer> parseIntList(String data) {
        String[] list = data.substring(1, data.length()-1).split(",");
        List<Integer> result = new ArrayList<>(list.length);
        for(int i=0; i<list.length; ++i) {
            if(list[i].trim().length() <= 0) break;
            result.add(Integer.parseInt(list[i].trim()));
        }
        return result;
    }
    public static List<String> parseStringList(String data) {
        String[] list = data.substring(1, data.length()-1).split(",");
        List<String> result = new LinkedList<>();
        for(int i=0; i<list.length; ++i) {
            result.add(list[i].substring(1, list[i].length()-1));
        }
        return result;
    }
    public static ListNode parseListNode(String data) {
        String[] list = data.substring(1, data.length()-1).split(",");
        ListNode head = new ListNode();
        ListNode tail = head;
        for(int i=0; i<list.length; ++i) {
            if(list[i].trim().length() <= 0) break;
            ListNode temp = new ListNode(Integer.parseInt(list[i].trim()));
            tail.next = temp;
            tail = temp;
        }
        return head.next;
    }

    public static ListNode[] parseListNodeArray(String data) {
        if (data.length() <= 3) {
            return new ListNode[] {};
        }
        String[] lists = data.substring(1, data.length() - 2).split("],");
        ListNode[] results = new ListNode[lists.length];
        for (int i = 0; i < lists.length; i++) {
            results[i] = ListUtil.parseListNode(lists[i] + "]");
        }
        return results;
    }

    public static void printListNode(ListNode head) {
        if(head == null) {
            System.out.println("This ListNode is null");
            return;
        }
        System.out.print(head.val);
        head = head.next;
        while(head != null) {
            System.out.print("->" + head.val);
            head = head.next;
        }
        System.out.print("\n");
    }
    public static void printIntArray(int[] nums) {
        System.out.print("[");
        for (int i = 0; i < nums.length; i++) {
            if(i>0) {
                System.out.print(",");
            }
            System.out.print(nums[i]);
        }
        System.out.println("]");
    }

    public static void printIntList(List<Integer> list) {
        int[] arr = new int[list.size()];
        for(int i=0; i<arr.length; ++i) {
            arr[i] = list.get(i);
        }
        printIntArray(arr);
    }

    public static void printIntListList(List<List<Integer>> lists) {
        for (List<Integer> list : lists) {
            printIntList(list);
        }
    }

    public static void printCharArray(char[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if(i>0) {
                System.out.print(",");
            }
            System.out.print(arr[i]);
        }
        System.out.println("]");
    }

    public static void printStringArray(String[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if(i>0) {
                System.out.print(",");
            }
            System.out.print(arr[i]);
        }
        System.out.println("]");
    }

    public static void printStringList(List<String> list) {
        String[] arr = new String[list.size()];
        for(int i=0; i<arr.length; ++i) {
            arr[i] = list.get(i);
        }
        printStringArray(arr);
    }
}
